import QQMapWX from '@/libs/qqmap-wx-jssdk.min.js';
export default {
  data() {
    return {
      name: '',
      latitude: '',
      longitude: '',
      address: '',
      qqmapsdk: null,
      key: 'SYXBZ-Y2D3M-35K6T-6WHRV-4E3N3-AAB6P',
    }
  },
   onShow() {
    let _this = this;
    if (uni.getStorageSync('location')) {
    	this.name = uni.getStorageSync('location').name;
      this.latitude = uni.getStorageSync('location').latitude;
      this.longitude = uni.getStorageSync('location').longitude;
    } else {
    	this.qqmapsdk = new QQMapWX({
    		key: this.key
    	});
    	uni.authorize({
    		scope: 'scope.userLocation',
    		success: res => {
    			console.log(res)
    			uni.getLocation({
    				type: 'wgs84',
            isHighAccuracy: true,
    				success(res) {
              console.log(res)
              _this.latitude = res.latitude;
              _this.longitude = res.longitude;
    					_this.getLocationByLonglat(res.latitude, res.longitude);
    				},
            fail(res) {
              console.log('失败2：', res);
            }
    			});
    		},
    		fail: res => {
    			console.log('失败：', res);
    		}
    	});
    }
  },
  methods: {
    //经纬度获取位置
    getLocationByLonglat: function(latitude, longitude) {
    	let _this = this;
    	// #ifdef H5
    	let url =
    		`https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude + "," + longitude}&key=${this.key}&callbackName=QQmap&output=jsonp&coord_type=5&get_poi=0`
    	this.tui.tuiJsonp(url, (res) => {
    		if (res.status === 0) {
    			
    		}
    	}, "QQmap")
    	// #endif
    
    	// #ifdef MP-WEIXIN
    	this.qqmapsdk.reverseGeocoder({
    		location: {
    			latitude: latitude,
    			longitude: longitude
    		},
    		success: function(res) {
    			console.log(res.result);
          _this.name = res.result.formatted_addresses.recommend;
          uni.setStorage({
            key: 'location',
            data: {
              latitude: latitude,
              longitude: longitude,
              name: res.result.formatted_addresses.recommend,   //结合知名地点形成的描述性地址，更具人性化特点
              address: res.result.address,
              city: res.result.address_component.city,

            }
          })
    		},
    		fail: res => {
    			console.log('解析具体位置失败：', res);
    		}
    	})
    	// #endif
    },
    //打开地图，选择位置
    chooseLocation() {
      if(uni.getStorageSync('location')) {
        this.latitude = uni.getStorageSync('location').latitude;
        this.longitude = uni.getStorageSync('location').longitude;
      }
      // console.log("latitude",this.latitude)
      // console.log("longitude",this.longitude)
      uni.chooseLocation({
        latitude: this.latitude,
        longitude: this.longitude,
      	success: res => {
          console.log("选择位置",res)
          var location = {
            address: res.address,
            latitude: res.latitude,
            longitude: res.longitude,
            name: res.name
          };
          uni.setStorageSync('location',location)
      	},
      	fail(res) {
      		uni.getSetting({
      			success: function(res) {
      				var statu = res.authSetting;
      				if (!statu['scope.userLocation']) {
      					uni.showModal({
      						title: '是否授权当前位置',
      						content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
      						success(tip) {
      							if (tip.confirm) {
      								uni.openSetting({
      									success: function(data) {
      										if (data.authSetting['scope.userLocation'] === true) {
      											this.$util.showToast({
      												title: '授权成功'
      											});
      											//授权成功之后，再调用chooseLocation选择地方
      											setTimeout(function() {
      												uni.chooseLocation({
      													success: data => {
      														this.address = res.address;
      														var location = {
      														  address: res.address,
      														  latitude: res.latitude,
      														  longitude: res.longitude,
      														  name: res.name
      														};
      														uni.setStorageSync('location',location)
      													}
      												});
      											}, 1000);
      										}
      									}
      								});
      							} else {
      								this.$util.showToast({
      									title: '授权失败'
      								});
      							}
      						}
      					});
      				}
      			}
      		});
      	}
      });
    },
  },
}